package domain.controller;

import java.io.IOException;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.data.QnAPersistenceManager;
import domain.model.InOutLink;

@SuppressWarnings("serial")
public class SearchLinkServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.getWriter().println("Hello, world");
        String ip = req.getRemoteAddr();

        req.setAttribute("ip", ip);        
        PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
       
		String title_url = req.getParameter("name");
		title_url = title_url.toLowerCase().replaceAll("(http|https)+://(www.)*", "").replaceAll("^[\\w\\W]+[\\/]{2}", "").replaceAll("[\\/]+[\\w\\W]+$", "").replaceAll("www.", "");
		
		req.setAttribute("domain", title_url);
		
		Query query2 = psm.newQuery(InOutLink.class);
		query2.setFilter("inLink =='"+title_url+"'");
		query2.setRange(0,10);
		@SuppressWarnings("unchecked")
		List<InOutLink> outlink = (List<InOutLink>) query2.execute();
		req.setAttribute("outlink", outlink);
		
		Query query3 = psm.newQuery(InOutLink.class);
		query3.setFilter("outLink =='"+title_url+"'");
		query3.setRange(0,10);
		@SuppressWarnings("unchecked")
		List<InOutLink> inlink = (List<InOutLink>) query3.execute();
		req.setAttribute("inlink", inlink);
		
		req.setAttribute("title", "Check cross link of website "+ title_url+" - domain-infomation.appspot.com");
		req.setAttribute("description", "Check cross link of website "+title_url+".sites that link to "+title_url+" website is synthesized by domain-infomation.appspot.com");
		req.setAttribute("keyword", title_url+",check link,link,link in,linkout,Domain name information,  Server information, Web site analysis, website traffic details, Website backlinks website IP, website ranking info");
		
        try {
			req.getRequestDispatcher("/check_link.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

}
